package client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import business.AccountInfo;
import business.BanKInfo;
import business.ObjectFactory;

public class TestMashalling {
	private static final String file_path = "ds_1_3.xml";
	public static Marshaller marshaller;
	public static Unmarshaller unmarshaller;
	public static BanKInfo bank; 
	
	// Constructor
	public static void BankImplementation()
	{
		
		try {
			// create the context on package
			JAXBContext jc = JAXBContext.newInstance("business");
			// create marshaller and unmarshaller using context
			unmarshaller = jc.createUnmarshaller();
			marshaller = jc.createMarshaller();
						
		  } 
		catch (JAXBException e) { e.printStackTrace(); }
	}
	
	private static void unmarshal()
	{
		JAXBElement<BanKInfo> wh = null;
		try {
			wh = (JAXBElement<BanKInfo>)unmarshaller.unmarshal(new FileInputStream(file_path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		bank = wh.getValue();
	}
	
	private static void marshal(BanKInfo bank)
	{
		
		ObjectFactory obf = new ObjectFactory();
		JAXBElement<BanKInfo> output = obf.createBank(bank);
	   try {
	   	marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
	   	marshaller.marshal(output, new FileOutputStream(file_path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {

			
			File f = new File(file_path);
			if (!f.exists()){
				System.out.println("File  does not exist");
				return;
			}		
			
			BankImplementation();
			
			unmarshal();
			System.out.println("Object unmarshalled " + bank.getAccount().size());
								
			AccountInfo account1 = new AccountInfo();
			account1.setAccountID("Mi Primera Cuenta");
			account1.setAmount(8000);
			
			AccountInfo account2 = new AccountInfo();
			account2.setAccountID("Mi Segunda Cuenta");
			account2.setAmount(1000);
			
			bank.getAccount().add(account1);
			bank.getAccount().add(account2);
			
			System.out.println("About to start the marshall process");
			marshal(bank);			
			System.out.println("Object marshalled");
			
			System.out.println("unmarshall process 2");
			unmarshal();
			System.out.println("Object unmarshalled " + bank.getAccount().size());
		} catch(Exception ex){
			ex.printStackTrace();
		}
	}

}
